import requests
import re
def zz():
    s1 = 1.1
    an = re.search('^\+?[1-9]{1}[0-9]{0,2}[0]$', str(s1))
    if an:
        print('yes')
    else:
        print('no')


reqUrl = "http://10.100.19.242:32421/cashloan-api"

header = {
    "Content-Type": "application/json",
    "Host": "10.100.19.242",
    "platform": "app",
    "User-Agent": "Mozilla//5.0(Linux;Android9;RedmiNote8ProBuild//PPR1.180610.011;wv)AppleWebKit//537.36(KHTML,likeGecko)Version//4.0Chrome//80.0.3987.99MobileSafari//537.36",
    "device": "android",
    "deviceVer": "RedmiNote8Pro[Android_9]",
    "locale": "flb",
    "appName": "HiCash",
    "appVer": "1.0.0",
    "appMarket": "googleplay",
    "Content-Length": "UTF-8"
}
usernames = ["09741570900","09741570901","09741570902","09741570903","09741570904"]
body = {
    "apiName": "token",
    "apiVer": "v1",
    "data": {
        "languages": "en",
        "passWord": "111111",
        "registerOsVersion": "Android_9",
        "userImei": "864795040155582",
        "userName": "",
        "xjdVersion": "1.0.0"
    },
    "sysName": "cashloan"
}



def sendRequest():
    for index in range(len(usernames)):
        body['data']['userName'] = usernames[index]
        resp = requests.post(reqUrl, json=body, headers=header)
        print(resp.json()['data']['token'])

#子回文
def zhw():
    a = 'abbc'
    arr = []
    for ex in a:
        arr.append(ex)
    for ind in range(len(arr)):
        print(arr.index(arr[ind]))
    print()


def test1():
    print("55555555")
    leaves = 'rrryyyrryyyrr'
    n = len(leaves)
    f = [[0, 0, 0] for _ in range(n)]
    f[0][0] = int(leaves[0] == "y")
    f[0][1] = f[0][2] = f[1][2] = float("inf")

    for i in range(1, n):
        isRed = int(leaves[i] == "r")
        isYellow = int(leaves[i] == "y")
        f[i][0] = f[i - 1][0] + isYellow
        f[i][1] = min(f[i - 1][0], f[i - 1][1]) + isRed
        if i >= 2:
            f[i][2] = min(f[i - 1][1], f[i - 1][2]) + isYellow

    print(f[n - 1][2])

#给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
def lengthOfLongestSubstring(s: str) -> int:
    lst = []
    n = len(s)
    ans = 0
    for i in range(n):
        #while是防止連續重複元素
        while s[i] in lst:
            del lst[0]  # 队首元素出队
        lst.append(s[i])  # 排除重复元素后 新元素入队
        ans = max(ans, len(lst))
    print(ans)
    return ans

def findMedianSortedArrays(nums1, nums2):
    nums1 = nums1 + nums2
    if len(nums1) < 1:
        return 0
    nums1.sort()
    le = len(nums1)
    if le%2 == 0:
        print('v:', (nums1[int(le/2) -1] + nums1[int(le/2)])/2)
        return float((nums1[int(le/2) -1] + nums1[int(le/2)])/2)
    else:
        print('v:', nums1[le/2])
        return float(nums1[le/2])

#取最大子回文（動態規劃）
def longestPalindrome(s: str) -> str:
    n = len(s)
    if n == 2:
        if s[0] == s[1]:
            return s
        else:
            return s[0]
    if n < 2:
        return s
    if n > 1000:
        return ''
    leftIndex = 0
    longest = 1
    longestList = []
    for i in range(n-1):
        for j in range(1+i,n):
            #正序跟倒序對比
            # if s[i: j+1] == s[j:i:-1] + s[i]:
            #判斷回文子串
            if valodPalindromic(s,i,j) == 0:
                print('i:',i,'j:',j)
                #1：長度 ，2起始位， 回文子串
                tmpList = [j - i + 1, i, s[i:j + 1]]
                longestList.append(tmpList)
                if (j-i+1) > longest:
                    #記錄起始位
                    leftIndex = i
                    #記錄長度
                    longest = j-i+1
    longestList.sort()
    print('strList',longestList)
    print('str',s[leftIndex:leftIndex+longest])
    return s[leftIndex: leftIndex+longest]


#驗證子串是否為回文串
def valodPalindromic(s:str, left:int, right:int):
    while left < right:
        if s[left] != s[right]:
            return 1
        left+=1
        right-=1
    return 0

#将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列
def convert(s: str, numRows: int) -> str:
        if numRows < 2:
            return s

        #创建numRows长度的空list
        res = ["" for _ in range(numRows)]
        #i为下标，flag为方向
        i, flag = 0, -1
        for c in s:
            res[i] += c
            if i == 0 or i == numRows - 1:
                flag = -flag
            i += flag
        print("".join(res))

#输出倒序整数
def reverse(x: int) -> int:
    b = ''
    flag = 1
    if x < 0:
        x = abs(x)
        flag = -1
    s = str(x)
    for a in s:
        b = a + b

    c = int(b)*flag
    if c > 2 ** 31 - 1 or c < -2 ** 31:
        return 0
    else:
        return c
#给你一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false 。回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。
def isPalindrome(x: int) -> bool:
    s = str(x)
    left, right = 0, len(s) - 1
    while left < right:
        if s[left] != s[right]:
            return False
        left += 1
        right -= 1
    return True

#罗马数字
def intToRoman(num: int) -> str:
    nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]

    index = 0
    res = ''
    while index < 13:
        # 注意：这里是等于号，表示尽量使用大的"面值"
        while num >= nums[index]:
            res += romans[index]
            num -= nums[index]
        index += 1
    print(res)
'''编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀，返回空字符串
解法1，便利对比'''
def longestCommonPrefix(strs):
    # 对比用
    temporaryStr = ''
    for i in range(len(strs)):
        if temporaryStr == '' and i == 0:
            temporaryStr = strs[i]
        else:
            # 暂存长度比短的元素
            teStr = ''
            # 相同前缀
            pStr = ''
            if len(temporaryStr) < len(strs[i]):
                teStr = temporaryStr
            else:
                teStr = strs[i]
            for j in range(len(teStr)):
                if strs[i][j] == temporaryStr[j]:
                    pStr = pStr + strs[i][j]
                else:
                    break
            temporaryStr = pStr
    print(":::", temporaryStr)

    return temporaryStr
#解法2，对比最大最小字符
def longestCommonPrefix2(strs):

    if not strs:
        return ""
    strs.sort()
    n = len(strs)
    a = strs[0]
    b = strs[n - 1]
    res = ""
    for i in range(len(a)):
        if i < len(b) and a[i] == b[i]:
            res += a[i]
        else:
            break
    return res
'''给你一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a，b，c ，使得 a + b + c = 0 ？请你找出所有和为 0 且不重复的三元组。

注意：答案中不可以包含重复的三元组。'''
def threeSum(nums):
    arr = []
    if len(nums) == 0:
        return arr
    for i in range(len(nums) - 1):
        for j in range(i + 1, len(nums)):
            if nums[i] == nums[j] == 0 and nums.count(0) > 2:
                if [0, 0, 0] not in arr:
                    arr.append([0, 0, 0])
                    break
            values = []
            sum = 0 - (nums[i] + nums[j])
            if sum in nums and (nums[i] != sum and nums[j] != sum):
                values.append(nums[i])
                values.append(nums[j])
                values.append(sum)
                values.sort()
                if values not in arr:
                    arr.append(values)

    return arr


if __name__ == '__main__':
    # lengthOfLongestSubstring('bbbbb')
    # findMedianSortedArrays(a,b)
    # longestPalindrome('aaaa')
    # valodPalindromic('aaaa',0,1)

    # a = 'cbbd'
    # print('str',a[0:4])
    # s = "PAYPALISHIRING"
    # convert(s,3)
    # reverse(-123)
    # intToRoman(250)
    # longestCommonPrefix(["flower","flow","flight"])
    threeSum([-1,0,1,2,-1,-4])



